package org.gdstash.ui.character;

import java.awt.event.ActionEvent;
import java.util.Iterator;
import org.gdstash.db.DBShrine;
import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharShrineList;
import java.util.LinkedList;
import org.gdstash.character.GDCharUID;
import java.util.List;
import javax.swing.border.TitledBorder;
import java.awt.Font;
import java.awt.event.ActionListener;
import javax.swing.border.Border;
import org.gdstash.util.GDMsgFormatter;
import javax.swing.BorderFactory;
import org.gdstash.ui.GDStashFrame;
import javax.swing.UIManager;
import java.awt.Component;
import java.awt.LayoutManager;
import java.awt.Container;
import javax.swing.GroupLayout;
import javax.swing.JCheckBox;
import org.gdstash.ui.util.GDAbstractShrinePane;

public class GDCharShrineAct7Pane extends GDAbstractShrinePane
{
    private JCheckBox cbFrostveilHighlands;
    private JCheckBox cbFreyollValley;
    private JCheckBox cbUlosSprings;
    private JCheckBox cbDreadWastes;
    private JCheckBox cbScarstoneBurialGrounds;
    private JCheckBox cbAuroraPeaks;
    private JCheckBox cbDrangGalFortress;
    private JCheckBox cbTempleOfNerf;
    private int difficulty;
    private boolean changed;
    
    public GDCharShrineAct7Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbFrostveilHighlands).addComponent(this.cbFreyollValley).addComponent(this.cbUlosSprings).addComponent(this.cbDreadWastes).addComponent(this.cbScarstoneBurialGrounds).addComponent(this.cbAuroraPeaks).addComponent(this.cbDrangGalFortress).addComponent(this.cbAuroraPeaks).addComponent(this.cbTempleOfNerf));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbFrostveilHighlands)).addGroup(layout.createParallelGroup().addComponent(this.cbFreyollValley)).addGroup(layout.createParallelGroup().addComponent(this.cbUlosSprings)).addGroup(layout.createParallelGroup().addComponent(this.cbDreadWastes)).addGroup(layout.createParallelGroup().addComponent(this.cbScarstoneBurialGrounds)).addGroup(layout.createParallelGroup().addComponent(this.cbAuroraPeaks)).addGroup(layout.createParallelGroup().addComponent(this.cbDrangGalFortress)).addGroup(layout.createParallelGroup().addComponent(this.cbTempleOfNerf));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        }
        else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbFreyollValley);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbUlosSprings);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbDreadWastes);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbScarstoneBurialGrounds);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbAuroraPeaks);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbDrangGalFortress);
        layout.linkSize(0, this.cbFrostveilHighlands, this.cbTempleOfNerf);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbFreyollValley);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbUlosSprings);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbDreadWastes);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbScarstoneBurialGrounds);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbAuroraPeaks);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbDrangGalFortress);
        layout.linkSize(1, this.cbFrostveilHighlands, this.cbTempleOfNerf);
    }
    
    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float)GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float)GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float)GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_ACT7"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbFrostveilHighlands == null) {
            (this.cbFrostveilHighlands = new JCheckBox()).setEnabled(false);
            this.cbFrostveilHighlands.addActionListener(new ChangeActionListener());
        }
        this.cbFrostveilHighlands.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_07_FROSTVEIL_HIGHLANDS"));
        this.cbFrostveilHighlands.setFont(fntCheck);
        if (this.cbFreyollValley == null) {
            (this.cbFreyollValley = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbFreyollValley.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_07_FREYOLL_VALLEY"));
        this.cbFreyollValley.setFont(fntCheck);
        if (this.cbUlosSprings == null) {
            (this.cbUlosSprings = new JCheckBox()).setEnabled(false);
            this.cbUlosSprings.addActionListener(new ChangeActionListener());
        }
        this.cbUlosSprings.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_07_ULOS_SPRINGS"));
        this.cbUlosSprings.setFont(fntCheck);
        if (this.cbDreadWastes == null) {
            (this.cbDreadWastes = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbDreadWastes.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_07_DREAD_WASTES"));
        this.cbDreadWastes.setFont(fntCheck);
        if (this.cbScarstoneBurialGrounds == null) {
            (this.cbScarstoneBurialGrounds = new JCheckBox()).setEnabled(false);
            this.cbScarstoneBurialGrounds.addActionListener(new ChangeActionListener());
        }
        this.cbScarstoneBurialGrounds.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_07_SCARSTONE"));
        this.cbScarstoneBurialGrounds.setFont(fntCheck);
        if (this.cbAuroraPeaks == null) {
            (this.cbAuroraPeaks = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbAuroraPeaks.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_07_AURORA_PEAKS"));
        this.cbAuroraPeaks.setFont(fntCheck);
        if (this.cbDrangGalFortress == null) {
            (this.cbDrangGalFortress = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbDrangGalFortress.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_07_DRANG_GAL"));
        this.cbDrangGalFortress.setFont(fntCheck);
        if (this.cbTempleOfNerf == null) {
            (this.cbTempleOfNerf = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbTempleOfNerf.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_SHRINE_07_TEMPLE_NERF"));
        this.cbTempleOfNerf.setFont(fntCheck);
    }
    
    @Override
    public boolean hasChanged() {
        return this.changed;
    }
    
    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }
    
    public void setSelected(final boolean selected) {
        this.cbFrostveilHighlands.setSelected(selected);
        this.cbFreyollValley.setSelected(selected);
        this.cbUlosSprings.setSelected(selected);
        this.cbDreadWastes.setSelected(selected);
        this.cbScarstoneBurialGrounds.setSelected(selected);
        this.cbAuroraPeaks.setSelected(selected);
        this.cbDrangGalFortress.setSelected(selected);
        this.cbTempleOfNerf.setSelected(selected);
    }
    
    @Override
    public List<GDCharUID> getShrineList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbFrostveilHighlands.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_SHATTERED_REALMS);
        }
        if (this.cbFreyollValley.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_TEMPLE_OSYR);
        }
        if (this.cbUlosSprings.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_KORVAN_OASIS);
        }
        if (this.cbDreadWastes.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_CAIRAN_DOCKS);
        }
        if (this.cbScarstoneBurialGrounds.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_SANDBLOWN_RUINS);
        }
        if (this.cbAuroraPeaks.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_SANCTUARY_HORRAN);
        }
        if (this.cbDrangGalFortress.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_RUINS_ABYD);
        }
        if (this.cbTempleOfNerf.isSelected() == found) {
            list.add(GDCharShrineList.UID_SHRINE_06_VALLEY_CHOSEN);
        }
        return list;
    }
    
    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        DBShrine.Info info = null;
        info = DBShrine.getShrineInfo("devotionshrineg09.dbr", this.difficulty);
        this.cbFrostveilHighlands.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg01.dbr", this.difficulty);
        this.cbFreyollValley.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg08.dbr", this.difficulty);
        this.cbUlosSprings.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg02.dbr", this.difficulty);
        this.cbDreadWastes.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg10.dbr", this.difficulty);
        this.cbScarstoneBurialGrounds.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg07.dbr", this.difficulty);
        this.cbAuroraPeaks.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg03.dbr", this.difficulty);
        this.cbDrangGalFortress.setEnabled(info.active);
        info = DBShrine.getShrineInfo("devotionshrineg04.dbr", this.difficulty);
        this.cbTempleOfNerf.setEnabled(info.active);
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRestoredShrinesList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharShrineList.UID_SHRINE_07_FROSTVEIL_HIGHLANDS)) {
                this.cbFrostveilHighlands.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_07_FREYOLL_VALLEY)) {
                this.cbFreyollValley.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_07_ULOS_SPRINGS)) {
                this.cbUlosSprings.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_07_DREAD_WASTES)) {
                this.cbDreadWastes.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_07_SCARSTONE)) {
                this.cbScarstoneBurialGrounds.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_07_AURORA_PEAKS)) {
                this.cbAuroraPeaks.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_07_DRANG_GAL)) {
                this.cbDrangGalFortress.setSelected(true);
            }
            if (uid.equals(GDCharShrineList.UID_SHRINE_07_TEMPLE_NERF)) {
                this.cbTempleOfNerf.setSelected(true);
            }
        }
    }
    
    private class ChangeActionListener implements ActionListener
    {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDCharShrineAct7Pane.this.changed = true;
        }
    }
}
